package com.freewater.springboot.demo.web.async;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class BusinessService {

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    public void doSomeBusiness(DeferredResult<String> deferredResult, String name) {
        threadPoolTaskExecutor.execute(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            deferredResult.setResult("hello async " + name);
            log.info("doSomeBusiness completed");
        });
    }
}
